import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { DictApi, IssueLine, Workorder, workstationAPI } from '#/api';

import { reactive } from 'vue';

import { DictResp, getSelectOptions } from '#/api';

const mes_workorder_sourcetype = reactive<DictApi.SelectOption[]>([]);
const processList = reactive<DictApi.SelectOption[]>([]);
const workshopList = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_workorder_sourcetype']).then((res) => {
  if (res.mes_workorder_sourcetype) {
    mes_workorder_sourcetype.push(...res.mes_workorder_sourcetype);
  }
});

/**
 * 获取下拉框数据，以及回填其他隐藏数据
 */
getSelectOptions('/mes/pro/process/listAll', 'processName', 'processId').then(
  (res) => {
    if (
      res && // 检查 res.options 是否为数组，如果是则使用扩展运算符合并，否则将其作为空数组处理
      Array.isArray(res.options)
    ) {
      processList.push(...res.options);
    }
  },
);
/**
 * 获取下拉框数据，以及回填其他隐藏数据
 */
getSelectOptions('/mes/md/workshop/listAll', 'workshopName', 'workshopId').then(
  (res) => {
    if (
      res && // 检查 res.options 是否为数组，如果是则使用扩展运算符合并，否则将其作为空数组处理
      Array.isArray(res.options)
    ) {
      workshopList.push(...res.options);
    }
  },
);

/**
 * 生产工单选择的表单字段配置
 */
export function useWorkorderScheam() {
  return [
    {
      component: 'Input',
      fieldName: 'workorderCode',
      label: '工单编码',
      labelWidth: 70,
      formItemClass: 'col-span-2',
      componentProps: {
        allowClear: true,
        placeholder: '请输入工单编码',
      },
    },
    {
      component: 'Input',
      labelWidth: 70,
      componentProps: {
        allowClear: true,
        placeholder: '请输入工单名称',
      },
      fieldName: 'workorderName',
      label: '工单名称',
      formItemClass: 'col-span-2',
      labelClass: ' text-[#606266] font-bold',
    },
    {
      component: 'Input',
      fieldName: 'productCode',
      labelWidth: 70,
      formItemClass: 'col-span-2',
      label: '产品编号',
      componentProps: {
        placeholder: '请输入产品编号',
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'productName',
      labelWidth: 70,
      formItemClass: 'col-span-2',
      label: '产品名称',
      componentProps: {
        placeholder: '请输入产品名称',
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'clientName',
      labelWidth: 70,
      formItemClass: 'col-span-2',
      label: '客户名称',
      componentProps: {
        placeholder: '请输入客户名称',
        allowClear: true,
      },
    },
  ];
}

/**
 * 获取生产工单选择的表格列配置
 */
export function useWorkorderColumns(): VxeTableGridOptions<Workorder.Row>['columns'] {
  return [
    {
      field: 'workorderId',
      fixed: 'left',
      type: 'radio',
      width: 50,
    },
    {
      field: 'workorderCode',
      fixed: 'left',
      title: '工单编码',
      width: 150,
    },
    {
      field: 'orderSource',
      title: '工单来源',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_workorder_sourcetype.map((option) => [
            option.value,
            option.label,
          ]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'sourceCode',
      title: '订单编号',
    },
    {
      field: 'productCode',
      title: '产品编号',
    },
    {
      field: 'productName',
      title: '产品名称',
    },
    {
      field: 'productSpc',
      title: '规格型号',
    },
    {
      field: 'unitName',
      title: '单位',
    },
    {
      field: 'quantity',
      title: '工单数量',
    },
    {
      field: 'clientCode',
      title: '客户编码',
    },
    {
      field: 'clientName',
      title: '客户名称',
    },

    {
      field: 'requestDate',
      title: '需求日期',
    },
  ];
}

/* 工作站表格列 */
export function useWorkstationColumns(): VxeTableGridOptions<workstationAPI.Row>['columns'] {
  return [
    { align: 'left', title: '', type: 'radio', width: 50 },
    {
      field: 'workstationCode',
      title: '工作站编号',
    },
    { field: 'workstationName', title: '工作站名称' },
    { field: 'workstationAddress', title: '工作站地点' },
    {
      field: 'workshopName',
      title: '所在车间名称',
    },
    { field: 'processName', title: '所属工序' },

    {
      field: 'remark',
      title: '备注',
    },
  ];
}
export function useWorkstationScheam() {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入工作站编码',
      },
      fieldName: 'workstationCode',
      label: '工作站编号',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: workshopList,
        placeholder: '请选择车间',
      },
      fieldName: 'workshopId',
      label: '所在车间名称',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: processList,
        placeholder: '请选择工序',
      },
      fieldName: 'processId',
      label: '所属工序',
      formItemClass: 'col-span-2',
    },
  ];
}

export function useIssueLineColumns(
  status: string,
): VxeTableGridOptions<IssueLine.Row>['columns'] {
  return [
    {
      field: 'issueId',
      fixed: 'left',
      type: status === 'PREPARE' ? 'checkbox' : 'expand',
      slots: { content: 'expand_content' },
      width: 50,
    },
    {
      field: 'itemCode',
      fixed: 'left',
      title: '物资编码',
      width: 150,
    },
    {
      field: 'itemName',
      fixed: 'left',
      title: '物资名称',
    },
    {
      field: 'specification',
      title: '规格型号',
    },
    {
      field: 'unitName',
      title: '单位',
    },
    {
      field: 'quantityIssued',
      title: '领料数量',
    },
    {
      field: 'remark',
      title: '备注',
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}

export function useIssueLineScheam() {
  return [
    {
      component: 'Input',
      fieldName: 'lineId',
      dependencies: {
        triggerFields: ['lineId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'itemId',
      dependencies: {
        triggerFields: ['itemId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'issueId',
      dependencies: {
        triggerFields: ['issueId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'itemCode',
      rules: 'required',
      label: '产品物料编码',
      labelWidth: 100,
      formItemClass: 'col-span-4',
      componentProps: {
        class: 'w-full',
      },
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入产品物料名称',
        readonly: true,
      },
      fieldName: 'itemName',
      label: '产品物料名称',
      formItemClass: 'col-span-4',
      labelClass: ' text-[#606266] font-bold',
    },
    {
      component: 'Input',
      fieldName: 'unitName',
      labelWidth: 50,
      formItemClass: 'col-span-4',
      label: '单位',
      componentProps: {
        placeholder: '',
        class: 'w-full',
        readonly: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'specification',
      labelWidth: 70,
      formItemClass: 'col-span-12',
      label: '规格型号',
      componentProps: {
        placeholder: '',
        class: 'w-full',
        readonly: true,
        rows: 2,
        showCount: false,
      },
    },
    {
      component: 'ElInputNumber',
      fieldName: 'quantityIssued',
      formItemClass: 'col-span-4',
      defaultValue: 1,
      labelWidth: 70,
      label: '领料数量',
    },
    {
      component: 'Textarea',
      formItemClass: 'col-span-12',
      componentProps: {
        placeholder: '请输入内容',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      labelWidth: 70,
    },
  ];
}
